package shuzi.activity;

import java.util.ArrayList;
import java.util.List;

import shuzi.adapter.AfterSaleAdapter;
import shuzi.data.Database;
import shuzi.model.AfterSale;
import shuzi.util.Webserice;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.mapapi.core.AMapException;
import com.amap.mapapi.geocoder.Geocoder;
import com.amap.mapapi.location.LocationManagerProxy;
import com.shuzi.offer.R;

public class AfterSaleActivity extends Activity implements LocationListener {

	private LocationManagerProxy locationManager = null;
	private TextView AdminArea;
	private TextView Locality;
	private Intent intent;
	private ListView afterSaleListView;
	private List<AfterSale> saleList = null;
	private ProgressBar progressBar = null;
	private AfterSaleAdapter adapter = null;
	Double geoLat;
	Double geoLng;

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case Database.HANDLER_REFRESH_GRIDVIEW:
				Geocoder geo = new Geocoder(AfterSaleActivity.this);
				try {
					List<Address> list = geo.getFromLocation(geoLat, geoLng, 1);
					for (Address a : list) {
						/*
						 * Log.v("省会", a.getAdminArea()); Log.v("城市",
						 * a.getLocality());
						 */
						// myLocation.setText(a.getAdminArea()+a.getLocality());
						AdminArea.setVisibility(View.VISIBLE);
						Locality.setVisibility(View.VISIBLE);
						AdminArea.setText(a.getAdminArea());
						Locality.setText(a.getLocality());
						if (saleList == null) {
							initData(a.getAdminArea(), a.getLocality());
						}
					}
				} catch (AMapException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;

			case Database.HANDLER_REFRESH_LISTVIEW:
				progressBar.setVisibility(View.GONE);
				saleList = (List<AfterSale>) msg.obj;
				adapter = new AfterSaleAdapter(AfterSaleActivity.this, saleList);
				afterSaleListView.setAdapter(adapter);
				break;
			case Database.HANDLER_REFRESH_NODATA:
				progressBar.setVisibility(View.GONE);
				Toast.makeText(AfterSaleActivity.this, "暂无数据",
						Toast.LENGTH_LONG).show();
				break;

		
			}

		
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.after_sale);
		// myLocation = (TextView) findViewById(R.id.myLocation);
		afterSaleListView = (ListView) findViewById(R.id.afterSaleListView);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		locationManager = LocationManagerProxy.getInstance(this);
		AdminArea = (TextView) findViewById(R.id.province);
		Locality = (TextView) findViewById(R.id.city);
		intent = getIntent();
	}

	public boolean enableMyLocation() {
		boolean result = true;
		Criteria cri = new Criteria();
		cri.setAccuracy(Criteria.ACCURACY_COARSE);
		cri.setAltitudeRequired(false);
		cri.setBearingRequired(false);
		cri.setCostAllowed(false);
		String bestProvider = locationManager.getBestProvider(cri, true);
		locationManager.requestLocationUpdates(bestProvider, 2000, 10, this);
		return result;
	}

	public void disableMyLocation() {
		locationManager.removeUpdates(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		enableMyLocation();
	}

	@Override
	protected void onPause() {
		disableMyLocation();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if (locationManager != null) {
			locationManager.removeUpdates(this);
			locationManager.destory();
		}
		locationManager = null;
		super.onDestroy();
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null) {
			geoLat = location.getLatitude();
			geoLng = location.getLongitude();
			String str = (":(" + geoLng + "," + geoLat + ")");
			Message msg = new Message();
			msg.obj = str;
			msg.what = Database.HANDLER_REFRESH_GRIDVIEW;
			if (handler != null) {
				handler.sendMessage(msg);
			}
		}
	}

	/**
	 * 初始化数据
	 */
	public void initData(final String province, final String city) {
		final String brandId = intent.getStringExtra(Database.brandId);
		saleList = new ArrayList<AfterSale>();
		new Thread(new Runnable() {

			@Override
			public void run() {
				/*Webserice.getServiceInfo(province, city, brandId, handler,
						Database.HANDLER_REFRESH_LISTVIEW);*/
				Webserice.getServiceInfo("广东", "深圳", "2", handler,
						Database.HANDLER_REFRESH_LISTVIEW);

			}
		}).start();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
